#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->listWidget->setAutoScroll(true);
    iSANServer = new SANServer();
    iSANSorter = new SANSorting(iSANServer);
    connect(iSANServer, SIGNAL(EmitClientAccepted(bool)), this, SLOT(ClientAccepted(bool)));
    connect(iSANServer, SIGNAL(EmitServerConnected(bool)), this, SLOT(NotifyServerConnected(bool)));
    connect(iSANServer, SIGNAL(EmitDataReceived(QString)), this, SLOT(DisplayRecdData(QString)));
    //connect(iSANServer, SIGNAL(EmitSortData()), iSANSorter, SLOT(SortAndSend()));
    connect(iSANSorter, SIGNAL(EmitServerToClient(bool)), this, SLOT(ServerToClient(bool)));
    connect(iSANSorter, SIGNAL(EmitDisplaySortedData(char*)), this, SLOT(DisplaySortedData(char*)));
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_pressed()
{
    if(iSANServer->isRunning() == true)
        iSANServer->quit();
    if(iSANSorter->isRunning() == true)
        iSANSorter->quit();
    delete iSANServer;
    delete iSANSorter;
    exit(0);
}

void MainWindow::on_pushButton_2_pressed()
{
    QString port = ui->lineEdit->text();
    int port_1 = port.toInt();
    port_1 = 9993;
    QString num = ui->lineEdit_2->text();
    int num_1 = num.toInt();
    iSANServer->SetServerPort(port_1);
    iSANServer->SetMaxClients(num_1);
    iSANServer->start();

}


void MainWindow::NotifyServerConnected(bool aStatus)
{
    QMessageBox temp;
    QString str("Awaiting Client connections!");
    if(aStatus == true)
        temp.setText("Server Started Successfully!");
    else
        temp.setText("Server Start Uncessful");
    temp.setStandardButtons(QMessageBox::Ok);
    int ret = temp.exec();
    switch(ret)
    {
        case QMessageBox::Ok:
            ui->page->hide();
            ui->page_4->show();
            ui->listWidget_2->addItem(str);
            ui->listWidget_2->scrollToBottom();
            break;
        default:
            break;
    }
    if(aStatus == false)
    {
        if(iSANServer->isRunning() == true)
            iSANServer->quit();
        if(iSANSorter->isRunning() == true)
            iSANSorter->quit();
        delete iSANServer;
        delete iSANSorter;
        exit(0);
    }
}

void MainWindow::ClientAccepted(bool aStatus)
{
    QMessageBox temp;
    QString str;
    if(aStatus == true)
        str = "Client Connected!";
    else
        str = "Client Not connected";
    temp.setText(str);
    temp.setStandardButtons(QMessageBox::Ok);
    int ret = temp.exec();
    switch(ret)
    {
        case QMessageBox::Ok:
            ui->listWidget->addItem(str);
            ui->listWidget->scrollToBottom();
            ui->page_4->hide();
            ui->page_3->show();
            break;
        default:
            break;
    }

}

void MainWindow::DisplayRecdData(QString aBuf)
{
    ui->listWidget->addItem(aBuf);
    ui->listWidget->scrollToBottom();

 }

void MainWindow::on_pushButton_3_pressed()
{
    QString num = QString::number(iSANServer->GetDroppedCount());
    num.append(" packets dropped! ");
    QString num_1 = QString::number((iSANServer->GetCount()));
    num_1.append(" packets received! ");
    QString num_2 = QString::number(iSANSorter->GetCount());
    num_2.append(" packets sorted! ");
    QString final = num + num_1 + num_2;
    QMessageBox temp;
    temp.setText(final);
    temp.setStandardButtons(QMessageBox::Ok);
    int ret = temp.exec();
    switch(ret)
    {
        case QMessageBox::Ok:
            if(iSANServer->isRunning() == true)
                iSANServer->quit();
            if(iSANSorter->isRunning() == true)
                iSANSorter->quit();
            delete iSANServer;
            delete iSANSorter;
            exit(0);
            break;

        default:
            break;
    }
}

void MainWindow::on_pushButton_5_pressed()
{
    if(iSANServer->isRunning() == true)
        iSANServer->quit();
    if(iSANSorter->isRunning() == true)
        iSANSorter->quit();
    delete iSANServer;
    delete iSANSorter;
    exit(0);
}

void MainWindow::on_pushButton_4_pressed()
{

    QString addr = ui->lineEdit_3->text();
    QString port = ui->lineEdit_4->text();
    int port_1 = port.toInt();
    std::string addr_1 = addr.toStdString();

    //addr_1 = "192.168.110.201";
    addr_1 = "127.0.0.1";
    iSANSorter->SetPort(port_1);
    iSANSorter->SetIp(addr_1);
    iSANSorter->ConnectSortingThread();

}

void MainWindow::ServerToClient(bool aStatus)
{
    QMessageBox temp;
    if(aStatus == true)
    {
        temp.setText("Server Connected to Client");
        iSANSorter->start();
    }
    else
        temp.setText("Server Not connected to Client");
    temp.setStandardButtons(QMessageBox::Ok);
    int ret = temp.exec();
    switch(ret)
    {
        case QMessageBox::Ok:
            ui->page_3->hide();
            ui->page_2->show();
            break;
        default:
            break;
    }

}

void MainWindow::DisplaySortedData(char *aBuf)
{
    QString temp(aBuf);
    ui->listWidget_3->addItem(temp);
    ui->listWidget_3->scrollToBottom();
}
